package com.senlan.water.dao.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.senlan.water.common.constant.DeleteStatusEnum;
import com.senlan.water.common.exception.BusinessException;
import com.senlan.water.common.result.ResultCode;
import com.senlan.water.common.util.DateUtil;
import com.senlan.water.common.util.UserInfoUtil;
import com.senlan.water.dao.domain.PolicyPublicity;
import com.senlan.water.dao.domain.User;
import com.senlan.water.dao.service.PolicyPublicityService;
import com.senlan.water.dao.mapper.PolicyPublicityMapper;
import com.senlan.water.dto.policy.PolicyPublicityAddDTO;
import com.senlan.water.dto.policy.PolicyPublicityEditDTO;
import com.senlan.water.dto.policy.PolicyPublicitySearchDTO;
import org.redisson.Redisson;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* @author 10499
* @description 针对表【policy_publicity(政策宣发)】的数据库操作Service实现
* @createDate 2024-12-03 15:25:29
*/
@Service
public class PolicyPublicityServiceImpl extends ServiceImpl<PolicyPublicityMapper, PolicyPublicity>
    implements PolicyPublicityService{

    @Resource
    private Redisson redisson;

    @Resource
    private UserInfoUtil userInfoUtil;

    @Override
    public Page<PolicyPublicity> getPage(PolicyPublicitySearchDTO searchDTO) {
        LambdaQueryWrapper<PolicyPublicity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(searchDTO.getTitle()),PolicyPublicity::getTitle,searchDTO.getTitle());
        if (CollectionUtil.isNotEmpty(searchDTO.getCreateTime())){
            queryWrapper.ge(PolicyPublicity::getCreateTime,searchDTO.getCreateTime().get(0));
            queryWrapper.le(PolicyPublicity::getCreateTime, DateUtil.convertDate(searchDTO.getCreateTime().get(1)));
        }


        Page<PolicyPublicity> page = new Page<>();
        page.setCurrent(searchDTO.getPageNum());
        page.setSize(searchDTO.getPageSize());
        baseMapper.selectPage(page,queryWrapper);
        return page;
    }

    @Override
    public Boolean addNew(PolicyPublicityAddDTO addDTO) {
        PolicyPublicity policyPublicity = new PolicyPublicity();
        BeanUtil.copyProperties(addDTO,policyPublicity);
        policyPublicity.setDeleteFlag(DeleteStatusEnum.DELETE_STATUS_NORMAL.getCode());

        //设置用户信息
        User userInfo = userInfoUtil.getUserInfo();
        policyPublicity.setCreateBy(userInfo.getNickname());
        policyPublicity.setUpdateBy(userInfo.getNickname());
        return baseMapper.insert(policyPublicity) > 0;
    }

    @Override
    public Boolean editById(PolicyPublicityEditDTO editDTO) {
        PolicyPublicity policyPublicity = baseMapper.selectById(editDTO.getId());
        if (policyPublicity == null){
            throw new BusinessException(ResultCode.POLICY_DATA_NOT_EXITS);
        }
        BeanUtil.copyProperties(editDTO,policyPublicity);

        //设置用户信息
        User userInfo = userInfoUtil.getUserInfo();
        policyPublicity.setUpdateBy(userInfo.getNickname());
        return baseMapper.updateById(policyPublicity) > 0;
    }

    @Override
    public Boolean deleteById(Long id) {
        PolicyPublicity policyPublicity = baseMapper.selectById(id);
        if (policyPublicity == null){
            throw new BusinessException(ResultCode.POLICY_DATA_NOT_EXITS);
        }
        policyPublicity.setDeleteFlag(DeleteStatusEnum.DELETE_STATUS_DELETED.getCode());

        //设置用户信息
        User userInfo = userInfoUtil.getUserInfo();
        policyPublicity.setUpdateBy(userInfo.getNickname());
        return baseMapper.deleteById(id) >0;
    }
}




